﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Views;
using NLBS.Views.Admin;

namespace NLBS.Web.UI
{
    public class UserGroupCommand : ICommand, IAction<UserGroup>
    {
        #region Presenter

        private UserGroupPresenter _presenter;

        public UserGroupPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new UserGroupPresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion

        private Action act;
        private string[] parameters;
        public UserGroupCommand(Action act, string[] parameters)
        {
            this.act = act;
            this.parameters = parameters;
            
        }

        #region ICommand Members

        public void Excute()
        {
            PresenterInit();
            switch (act)
            {
                case Action.Insert:
                    UpdateAction();
                    if (parameters.Length > 4)
                    {
                        InsertAction();
                    }
                    break;
                case Action.Delete:
                    DeleteAction();
                    break;

            }
        }

        private void InsertAction()
        {
            string[] newGroupList = parameters[4].Split(new char[] { ',' });
            UserGroup newGroup = new UserGroup();
            newGroup.Name = newGroupList[0];
            newGroup.Rights = newGroupList[1];
            this.Entity = newGroup;
            this.DataInsert(null, null);
        }

        private void DeleteAction()
        {
            for (int i = 1; i < parameters.Length; i++)
            {
                if (i > 0)
                {
                    this.keyID = Convert.ToInt32(parameters[i]);
                    this.DataDelete(null, null);
                }
            }
        }

        private void UpdateAction()
        {
            string[] selectList = parameters[1].Split(new char[] { ',' });
            string[] nameList = parameters[2].Split(new char[] { ',' });
            string[] rightsList = parameters[3].Split(new char[] { ',' });

            for (int i = 0; i < selectList.Length; i++)
            {
                this.keyID=Convert.ToInt32(selectList[i]);
                this.DataSelectSingle(null, null);
                UserGroup updateGroup = this.Entity;
                updateGroup.GroupID = Convert.ToInt32(selectList[i]);
                updateGroup.Name = nameList[i];
                updateGroup.Rights = rightsList[i];
                this.DataUpdate(null, null);
            }
        }

        #endregion



        #region IAction<UserGroup> Members

        private UserGroup _entity;
        public UserGroup Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }

        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataSelectSingle;

        #endregion
    }

}
